#!/bin/bash
echo  "Initializing... "
#iinput_file="$1"
echo "*$1*"
mkdir "$1"
echo  "Directory  "$1 "was created" 

if [ $# -lt 2 ]; then
    echo "Usage: $0 my_sample ROUND"
    exit 1
fi

BASE=$(pwd)

if [ $2 == "1" ]; then
    cp $1.v ./$1
    cd ./$1
    ./../new_cdc.py $1.v >  $1_cdc_paths.txt # the output is in new.v
    ./../insert_mux_p1 new.v tmp_out1.txt tmp_out2.txt # inout.name is used to generate toptester.v
    ./../insert_mux_p2 tmp_out1.txt tmp_out2.txt tmp_out3.txt #$1_changed.v is used by modelsim
    ./../insert_mux_p3 tmp_out3.txt $1_mux_inserted_fastscan.v $1_mux_inserted_modelsim.v 
    cp new.v $1_mux_inserted_fastscan.v
    # we need this in temporary to change sender_clk into clk_i
    # because we can have only one clock for dofile
    # Apr. 7
    ./../oneclock.py $1_mux_inserted_fastscan.v sender_clk clk_i
    mv $1_mux_inserted_fastscan.v.new $1_mux_inserted_fastscan.v
    ./../oneclock.py $1_mux_inserted_modelsim.v sender_clk clk_i
    mv $1_mux_inserted_modelsim.v.new $1_mux_inserted_modelsim.v

    # select sender dff and recv dff from cdc_path.txt
    python ./../sel_recv_dff.py $1_cdc_paths.txt clk_i
    # generate do files and f2 files and run files
    python ./../gen_dofile.py ${1}_mux_inserted_fastscan.v

    DO_DIR="$(pwd)/dofiles"
    RUN_DIR="$(pwd)/runfiles"

    #fastscan
    num=$(ls -l ./runfiles/ | wc -l)
    num=$((${num} - 1))
    echo ${num}

    # create dir for results provided by fastscan
    mkdir -p "fs_results"
    cd ./fs_results

    # we need to run every run file one by one
    # and before that, create a new directory for each of them
    # copy do file and run file and f2 file into that directory
    for ((i=1; i<=num; i++))
    do
        echo "[Fastscan] Processing ${i}"
        mkdir -p ${i}/log
        cd ${i}
        cp "${DO_DIR}/${i}.do" ./${i}.do
        cp "${DO_DIR}/f2_${i}" ./f2_${i}
        cp "${RUN_DIR}/run_${i}.py" "./run_${i}.py"
        python "./run_${i}.py"
        echo "[Fastscan] Done ${i}" 
        ${BASE}/extract_pi_ffs ${i}.pattern msr1_${i}.pattern # num.pattern is generated by zhiqiu file as an output of fastscan and msr1_num.pattern is used by modelsim as the input value

        echo "Generating bench for ModelSim..."
        # given the .v file, output test bench for modelsim
        # "Usages: %s VERILOG PATTERN STRUCT SCANCHAIN" 
        
        python ${BASE}/gen_ms.py ${BASE}/$1/$1_mux_inserted_modelsim.v msr1_${i}.pattern
        echo "Done."
        cd ../
    done

    echo  "Initializing Modelsim "
    cd ~/mgc
    rm -R work
    vlib work
    vmap work work
    vlog ${BASE}/GSCLib_3.0_modified.v
    vlog ${BASE}/$1/${1}_mux_inserted_modelsim.v

    for ((i=1; i<=num; i++))
    do
        for f in ${BASE}/$1/fs_results/${i}/bench/*; do 
           # vlog $f;
            vlog $f
            echo "[Modelsim Round 1] Processing ${i}"
            #vlog bench file
            vsim -c -do "run 40ns;quit" work.TopTester
            echo "[Modelsim Round 1] Done."
        done;
    done;

    #for ((i=1; i<=num; i++))
    # for ((j=1; j<=pattern_num; j++))
    #do
    #done

    #for (( i=0; i<=$2; i++))  # it is fault number
    #do
    #cd ..
    #./myfile.run



    #done
    #rm tmp_out1.txt
    #rm tmp_out2.txt
    echo "[done]"
else
    cd ${BASE}
    mkdir ${1}_2

    echo $((pwd))
    cp $1.v ./$1_2
    cd ./$1_2
    ./../new_cdc.py $1.v >  $1_cdc_paths.txt # the output is in new.v
    ./../insert_mux_p1 new.v tmp_out1.txt tmp_out2.txt # inout.name is used to generate toptester.v
    ./../insert_mux_p2 tmp_out1.txt tmp_out2.txt tmp_out3.txt #$1_changed.v is used by modelsim
    ./../insert_mux_p3 tmp_out3.txt $1_mux_inserted_fastscan.v $1_mux_inserted_modelsim.v
    cp new.v $1_mux_inserted_fastscan.v
    # we need this in temporary to change sender_clk into clk_i
    # because we can have only one clock for dofile
    # Apr. 7
    ./../oneclock.py $1_mux_inserted_fastscan.v sender_clk clk_i
    mv $1_mux_inserted_fastscan.v.new $1_mux_inserted_fastscan.v
    ./../oneclock.py $1_mux_inserted_modelsim.v sender_clk clk_i
    mv $1_mux_inserted_modelsim.v.new $1_mux_inserted_modelsim.v

    # select sender dff and recv dff from cdc_path.txt
    python ./../sel_recv_dff.py $1_cdc_paths.txt clk_i
    # generate do files and f2 files and run files
    python ./../gen_dofile_stuck_at.py ${1}_mux_inserted_fastscan.v

    DO_DIR="$(pwd)/dofiles"
    RUN_DIR="$(pwd)/runfiles"

    #fastscan
    num=$(ls -l ./runfiles/ | wc -l)
    num=$((${num} - 1))
    echo ${num}

    # create dir for results provided by fastscan
    mkdir -p "fs_results"
    cd ./fs_results

    # we need to run every run file one by one
    # and before that, create a new directory for each of them
    # copy do file and run file and f2 file into that directory
    for ((i=1; i<=num; i++))
    do
        echo "[Fastscan] Processing ${i}"
        mkdir -p ${i}/log
        cd ${i}
        cp "${DO_DIR}/${i}.do" ./${i}.do
        cp "${DO_DIR}/f2_${i}" ./f2_${i}
        cp "${RUN_DIR}/run_${i}.py" "./run_${i}.py"
        python "./run_${i}.py"
        echo "[Fastscan] Done ${i}"
        ${BASE}/extract_pi_ffs ${i}.pattern msr1_${i}.pattern # num.pattern is generated by zhiqiu file as an output of fastscan and msr1_num.pattern is used by modelsim as the input value

        echo "Generating bench for ModelSim..."
        # given the .v file, output test bench for modelsim
        # "Usages: %s VERILOG PATTERN STRUCT SCANCHAIN"

        # round 1
        python ${BASE}/gen_ms.py ${BASE}/$1_2/$1_mux_inserted_modelsim.v msr1_${i}.pattern  
        echo "Done."
        cd ../
    done

    echo  "Initializing Modelsim "
    #vlib work
    #vmap work work
    cd ~/mgc
    vlog ${BASE}/GSCLib_3.0_modified.v
    vlog ${BASE}//${1}_2/${1}_mux_inserted_modelsim.v

    for ((round=1; round<=3; ++round))
    do
        echo "Generating bech for Round ${round}"
        for ((i=1; i<=num; i++))
        do
            python ${BASE}/gen_ms_extra.py ${BASE}/$1_2/fs_results/${i}/ ${round}
        done;
        echo "Done."

        echo "Running ModelSim"
        for ((i=1; i<=num; i++))
        do
            for f in ${BASE}/$1_2/fs_results/${i}/bench_r${round}/*; do 
                echo ${f}
                vlog $f;
                echo "[Modelsim Round 1] Processing ${i}"
                #vlog bench file
                vsim -c -do "run 40ns;quit" work.TopTester
                echo "[Modelsim Round 1] Done."
            done;
        done;
        echo "Done."
    done;





    #for ((i=1; i<=num; i++))
    # for ((j=1; j<=pattern_num; j++))
    #do
    #done

    #for (( i=0; i<=$2; i++))  # it is fault number
    #do
    #cd ..
    #./myfile.run
    echo "[done]"
fi

